// @ts-nocheck
import http from '@ohos.net.http';
export default {
    data: {
        title: 'World',
        url: "https://jsonplaceholder.typicode.com/posts",
    },
    request(){
        let httpRequest = http.createHttp();
        httpRequest.request(this.url,({result,responseCode,header}) => {
            console.log(`result:${JSON.stringify(result)}`)
            console.log(`responseCode:${responseCode}`)
            console.log(`result:${JSON.stringify(header)}`)
//            console.log(data)
//            if (err == null) {
//                console.info('Result:' + data.result);
//                console.info('code:' + data.responseCode);
//                console.info('header:' + data.header);
//            } else {
////                console.info('error:' + err.data);
//            }
        });
    },
    requestOption(){
        let httpRequest= http.createHttp();
        httpRequest.request(this.url,
            {
                method: 'GET',
                header: {
                    'Content-Type': 'application/json'
                },
                extraData: {
                    key: "akey",
                    value:"new_avalue",
                    type:"String"
                },
                readTimeout: 6000,
                connectTimeout: 6000
            },({result,responseCode,header}) => {
                console.log(`result:${JSON.stringify(result)}`)
                console.log(`responseCode:${responseCode}`)
                console.log(`result:${JSON.stringify(header)}`)
//                console.log(err)
//                if (err == null) {
//                    console.info('Result:' + data.result);
//                    console.info('code:' + data.responseCode);
//                    console.info('header:' + data.header);
//                } else {
//                    console.info('error:' + err.data);
//                }
            });
    },
    requestPromise(){
        let httpRequest= http.createHttp();
        let promise = httpRequest.request(this.url, {
            method: "GET",
            connectTimeout: 6000,
            readTimeout: 6000,
            header: {
                'Content-Type': 'application/json'
            }
        });
        promise.then((value) => {
            console.info('Result:' + value.result);
            console.info('code:' + value.responseCode);
            console.info('header:' + value.header);
        }).catch((err) => {
            console.error(`errCode:${err.code}, errMessage:${err.data}`);
        });
    },
    destroyRequest(){
        console.log('进入destroy');
        let httpRequest= http.createHttp();
        httpRequest.on('headerReceive', (error, data) => {
            console.log('headerReceive')
            console.log(JSON.stringify(error))
            console.info('header: ' + data.header);
        });
//       setTimeout(()=>{
//
//       },1500)
        httpRequest.destroy();
        httpRequest.off('headerReceive',(error, data)=>{
            console.log('销毁请求')

        });

    }
}
